package cn.aj.controller;

import cn.aj.block.UserBlock;
import cn.aj.domain.User;
import cn.aj.fallback.UserFallback;
import com.alibaba.csp.sentinel.annotation.SentinelResource;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/user")
@RefreshScope // 开启动态刷新
public class UserController {
    @Value("${key.value}")
    private String value;

    @GetMapping(value = "/{id}")
    @SentinelResource(value = "loadUser",
            fallback = "loadUserFallback", fallbackClass = UserFallback.class,
            blockHandler = "loadUserBlockHandler", blockHandlerClass = UserBlock.class)
    public User loadUser(@PathVariable Long id) {
        System.out.println(1/0);
        return new User(id, "么么叽", "今天吃老鸭汤=======" + value);
    }

    /**
    * @Title: loadUserFallback
    * @Description: TODO
    * @Author: Mr.GT
    * @Version: 1.0
    * @Date:  2022/12/24 0024 20:52
    * @Parameters: [id]
    * @Return cn.aj.domain.User
     * 方法熔断
    */
//    public User loadUserFallback(@PathVariable Long id) {
//        return new User(-1L, "我是loadUserFallback", "我是方法熔断");
//    }

    /**
    * @Title: loadUserFallback
    * @Description: TODO
    * @Author: Mr.GT
    * @Version: 1.0
    * @Date:  2022/12/24 0024 20:59
    * @Parameters: [id]
    * @Return cn.aj.domain.User
     * 方法限流
    */
    public User loadUserBlockHandler(@PathVariable Long id) {
    return new User(-1L, "方法限流", "方法限流");
}


}
